import 'dart:convert';
import 'dart:typed_data';

import '../../../base/enum.dart';
import '../../../tools/byte_builder.dart';
import '../lorin_request_body.dart';

// Lorin公共请求体
class LorinPublicRequestBody {
  // 检查是否需要建立安全连接
  static LorinRequestBody get checkSafeConnectionResult => LorinRequestBody(
        "CheckSafeConnectionResult",
        channel: LorinDataChannelType.channel0Link,
        property: LorinDataPropertyType.property2,
      );

  // 建立安全连接
  static LorinRequestBody establishSafeConnection(Uint8List bytes) {
    return LorinRequestBody(
      "EstablishSafeConnection",
      channel: LorinDataChannelType.channel0Link,
      property: LorinDataPropertyType.property2,
      param: bytes,
    );
  }

  // 设备sn
  static LorinRequestBody get getSN => LorinRequestBody(
        "GetSN",
        channel: LorinDataChannelType.channel2Info,
        property: LorinDataPropertyType.property2,
      );

  // 产品模型 productModel
  static LorinRequestBody get getProductModel => LorinRequestBody(
        "GetProductModel",
        channel: LorinDataChannelType.channel2Info,
        property: LorinDataPropertyType.property1,
      );

  // 获取固件版本
  static LorinRequestBody getFirmwareVersion({
    int? targetDevice,
  }) =>
      LorinRequestBody(
        "GetFirmwareVersion",
        channel: LorinDataChannelType.channel2Info,
        property: LorinDataPropertyType.property6,
        target: targetDevice ?? 0x00, // 广播
      );

  // 开始固件升级
  static LorinRequestBody startFirmwareUpdate(int targetDevice,
      {bool isStart = true}) {
    return LorinRequestBody(
      "StartFirmwareUpdate",
      channel: LorinDataChannelType.channel3Control,
      property: LorinDataPropertyType.property4,
      // bytes[0] Uint8 启停控制 、 Uint8 升级方式
      // bytes[1]  按默认方式升级
      param: Uint8List.fromList([isStart ? 1 : 0, 0]),
      target: targetDevice,
    );
  }

  // 查询固件升级状态
  static LorinRequestBody queryFirmwareUpdateStatus(
    int targetDevice,
  ) {
    return LorinRequestBody(
      "QueryFirmwareUpdateStatus",
      channel: LorinDataChannelType.channel3Control,
      property: LorinDataPropertyType.property4,
      target: targetDevice,
    );
  }

  // 查询强制固件升级状态
  static LorinRequestBody queryForceFirmwareUpdateStatus({int? targetDevice}) {
    return LorinRequestBody(
      "QueryForceFirmwareUpdateStatus",
      channel: LorinDataChannelType.channel3Control,
      property: LorinDataPropertyType.property5,
      target: targetDevice,
    );
  }

  // 清除固件升级状态
  static LorinRequestBody clearFirmwareUpdateStatus(
    int forceUpgradeStatus,
    int lowVersion,
    int highVersion, {
    int? targetDevice,
  }) {
    final builder = ByteBuilder();
    builder.addUint8(forceUpgradeStatus);
    builder.addUint32(lowVersion);
    builder.addUint32(highVersion);
    return LorinRequestBody(
      "ClearFirmwareUpdateStatus",
      channel: LorinDataChannelType.channel3Control,
      property: LorinDataPropertyType.property6,
      param: builder.toBytes(),
      target: targetDevice,
    );
  }

  // 开始强制固件升级
  static LorinRequestBody startForceFirmwareUpdate(int targetDevice,
      int forceUpgradeStatus, int lowVersion, int highVersion) {
    final builder = ByteBuilder();
    builder.addUint8(forceUpgradeStatus);
    builder.addUint32(lowVersion);
    builder.addUint32(highVersion);
    return LorinRequestBody(
      "StartForceFirmwareUpdate",
      channel: LorinDataChannelType.channel3Control,
      property: LorinDataPropertyType.property4,
      param: builder.toBytes(),
      target: targetDevice,
    );
  }

  // 打开文件
  static LorinRequestBody openFile(
      int targetDevice, int openFileType, String path) {
    final builder = ByteBuilder();
    builder.addUint8(openFileType);
    builder.addBytes(utf8.encode(path));
    builder.addUint8(0);
    return LorinRequestBody(
      "OpenFile",
      channel: LorinDataChannelType.channel4File,
      property: LorinDataPropertyType.property1,
      param: builder.toBytes(),
      target: targetDevice,
    );
  }

  // 读写文件
  static LorinRequestBody readWriteFile(
    int targetDevice,
    int handle,
    int offset, {
    int? length,
    Uint8List? data,
  }) {
    final builder = ByteBuilder();
    builder.addUint8(handle);
    builder.addUint32(offset);
    if (length != null) {
      builder.addUint8(length);
    }
    if (data != null) {
      builder.addBytes(data);
    }
    return LorinRequestBody(
      "ReadWriteFile",
      channel: LorinDataChannelType.channel4File,
      property: LorinDataPropertyType.property2,
      param: builder.toBytes(),
      target: targetDevice,
      isWrite: data != null,
    );
  }

  // 校验文件
  static LorinRequestBody verifyFile(
    int targetDevice,
    int handle,
    int offset, {
    int? fileSize,
    int? crc32,
  }) {
    bool isWrite = fileSize != null && crc32 != null;
    final builder = ByteBuilder();
    if (isWrite) {
      builder.addUint8(handle);
      builder.addUint32(fileSize);
      builder.addUint32(offset);
      builder.addUint32(crc32);
    } else {
      builder.addUint8(handle);
      builder.addUint32(offset);
    }
    return LorinRequestBody(
      isWrite ? "VerifyWriteFile" : "VerifyReadFile",
      channel: LorinDataChannelType.channel4File,
      property: LorinDataPropertyType.property3,
      param: builder.toBytes(),
      target: targetDevice,
      isWrite: isWrite,
    );
  }

  // 关闭文件（终止下载）
  static LorinRequestBody closeFile(int targetDevice, int handle) {
    final builder = ByteBuilder();
    builder.addUint8(handle);
    return LorinRequestBody(
      "CloseFile",
      channel: LorinDataChannelType.channel4File,
      property: LorinDataPropertyType.property4,
      param: builder.toBytes(),
      target: targetDevice,
    );
  }

  // 读取文件信息
  static LorinRequestBody readFileInfo(int targetDevice, String path) {
    final builder = ByteBuilder();
    builder.addBytes(utf8.encode(path));
    builder.addUint8(0);
    return LorinRequestBody(
      "ReadFileInfo",
      channel: LorinDataChannelType.channel4File,
      property: LorinDataPropertyType.property5,
      param: builder.toBytes(),
      target: targetDevice,
    );
  }

  // 删除文件
  static LorinRequestBody deleteFile(int targetDevice, String path) {
    final builder = ByteBuilder();
    builder.addBytes(utf8.encode(path));
    builder.addUint8(0);
    return LorinRequestBody(
      "DeleteFile",
      channel: LorinDataChannelType.channel4File,
      property: LorinDataPropertyType.property6,
      param: builder.toBytes(),
      target: targetDevice,
    );
  }

  // 重命名文件
  static LorinRequestBody renameFile(
      int targetDevice, String oldPath, String newPath) {
    final builder = ByteBuilder();
    builder.addBytes(utf8.encode("$oldPath>$newPath"));
    builder.addUint8(0);
    return LorinRequestBody(
      "RenameFile",
      channel: LorinDataChannelType.channel4File,
      property: LorinDataPropertyType.property7,
      param: builder.toBytes(),
      target: targetDevice,
    );
  }

  // 获取文件列表
  static LorinRequestBody getFileList(
    int targetDevice,
    int index,
    int method,
    int depth,
    String path,
    String name,
  ) {
    final builder = ByteBuilder();
    builder.addUint16(index);
    builder.addBytes(utf8.encode('$method|$depth|${path}|${name}'));
    builder.addUint8(0);

    return LorinRequestBody(
      "GetFileList",
      channel: LorinDataChannelType.channel4File,
      property: LorinDataPropertyType.property8,
      param: builder.toBytes(),
      target: targetDevice,
    );
  }
}
